

<title>路由管理</title>
  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">控制台</a>
    <a><cite>路由管理</cite></a>
  </div>
</div>

<div class="layui-fluid">   
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
      </div>
      <table id="back-permission-list" lay-filter="back-permission-list"></table>  
      <script type="text/html" id="buttonSwitch">
        <input type="checkbox" name="" lay-skin="switch" lay-text="开启|禁用" lay-filter="status" value="{{d.id}}" 
        {{ d.status == 1 ? 'checked' : '' }}>
      </script>
      <script type="text/html" id="back-permission-list-operation">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
layui.use('permission', layui.factory('permission')).use(['admin', 'permission', 'table','jquery','treeGrid'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form
  ,setter = layui.setter
  ,treeGrid = layui.treeGrid;

  //监听 状态switch 
  form.on('switch(status)', function(data){
    if(data.elem.checked == true){
      admin.req({
        url:setter.api_back_permission_update,
        data:{'id':data.value,'status':1},
        done:function(res){
          layer.msg(res.msg);
          treeGrid.reload('back-permission-list'); //重载表格
        }
        ,faile:function(res) {
          treeGrid.reload('back-permission-list'); //重载表格
        }
      });
    }else{
      admin.req({
        url:setter.api_back_permission_update,
        data:{'id':data.value,'status':0},
        done:function(res){
          layer.msg(res.msg);
          treeGrid.reload('back-permission-list'); //重载表格
        }
        ,faile:function(res) {
          treeGrid.reload('back-permission-list'); //重载表格
        }
      });
    }
  });

  //事件
  var active = {
    add: function(){
      admin.popup({
        title: '添加路由'
        ,area: admin.screen() < 2 ? ['100%','530px'] : ['450px','530px']
        ,id: 'back-permission-add-popup'
        ,success: function(layero, index){
          view(this.id).render('role/permissionform').done(function(){
            // 上级选择
            admin.req({
              url: setter.api_back_permission_list
              ,done: function(res){
                var list = res.data.list;
                list.unshift({"id":0,"pid":0,"name":"空","status":1})
                $.each(list, function (index, item) {
                  if(item.pid == 0 && item.status == 1){
                    $('#back-permission-form-permission-select').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                  }
                });
                form.render(null, 'back-permission-form');
              }
            });
            form.on('submit(back-permission-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              field.status = field.status?1:0;
              admin.req({
                url: setter.api_back_permission_create
                ,data: field
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭
                  treeGrid.reload('back-permission-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });
    }
  } 
  $('.layui-btn.layuiadmin-btn-role').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>